home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python151_Src.lha / Python1.5_Source / Parser / myreadline.c < prev    next >
C/C++ Source or Header  |  1998-01-14  |  4KB  |  161 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Readline interface for tokenizer.c and [raw_]input() in bltinmodule.c.
  33.    By default, or when stdin is not a tty device, we have a super
  34.    simple my_readline function using fgets.
  35.    Optionally, we can use the GNU readline library.
  36.    my_readline() has a different return value from GNU readline():
  37.    - NULL if an interrupt occurred or if an error occurred
  38.    - a malloc'ed empty string if EOF was read
  39.    - a malloc'ed string ending in \n normally
  40. */
  41.  
  42. #define Py_USE_NEW_NAMES 1
  43.  
  44. #include "config.h"
  45.  
  46. #include <stdio.h>
  47. #include <string.h>
  48. #include <errno.h>
  49.  
  50. #include "myproto.h"
  51. #include "mymalloc.h"
  52. #include "intrcheck.h"
  53. #include "pythonrun.h"
  54.  
  55. #include "protos/myreadline_protos.h"
  56.  
  57. int (*PyOS_InputHook)Py_PROTO((void)) = NULL;
  58.  
  59. /* This function restarts a fgets() after an EINTR error occurred
  60.    except if PyOS_InterruptOccurred() returns true. */
  61.  
  62. static int
  63. my_fgets(buf, len, fp)
  64.     char *buf;
  65.     int len;
  66.     FILE *fp;
  67. {
  68.     char *p;
  69.     for (;;) {
  70.         if (PyOS_InputHook != NULL)
  71.             (void)(PyOS_InputHook)();
  72.         errno = 0;
  73.         p = fgets(buf, len, fp);
  74.         if (p != NULL)
  75.             return 0; /* No error */
  76.         if (feof(fp)) {
  77.             return -1; /* EOF */
  78.         }
  79. #ifdef EINTR
  80.         if (errno == EINTR) {
  81.             if (PyOS_InterruptOccurred()) {
  82.                 return 1; /* Interrupt */
  83.             }
  84.             continue;
  85.         }
  86. #endif
  87.         if (PyOS_InterruptOccurred()) {
  88.             return 1; /* Interrupt */
  89.         }
  90.         return -2; /* Error */
  91.     }
  92.     /* NOTREACHED */
  93. }
  94.  
  95.  
  96. /* Readline implementation using fgets() */
  97.  
  98. char *
  99. PyOS_StdioReadline(prompt)
  100.     char *prompt;
  101. {
  102.     int n;
  103.     char *p;
  104.     n = 100;
  105.     if ((p = malloc(n)) == NULL)
  106.         return NULL;
  107.     fflush(stdout);
  108.     if (prompt)
  109.         fprintf(stderr, "%s", prompt);
  110.     fflush(stderr);
  111.     switch (my_fgets(p, n, stdin)) {
  112.     case 0: /* Normal case */
  113.         break;
  114.     case 1: /* Interrupt */
  115.         free(p);
  116.         return NULL;
  117.     case -1: /* EOF */
  118.     case -2: /* Error */
  119.     default: /* Shouldn't happen */
  120.         *p = '\0';
  121.         break;
  122.     }
  123. #ifdef MPW
  124.     /* Hack for MPW C where the prompt comes right back in the input */
  125.     /* XXX (Actually this would be rather nice on most systems...) */
  126.     n = strlen(prompt);
  127.     if (strncmp(p, prompt, n) == 0)
  128.         memmove(p, p + n, strlen(p) - n + 1);
  129. #endif
  130.     n = strlen(p);
  131.     while (n > 0 && p[n-1] != '\n') {
  132.         int incr = n+2;
  133.         p = realloc(p, n + incr);
  134.         if (p == NULL)
  135.             return NULL;
  136.         if (my_fgets(p+n, incr, stdin) != 0)
  137.             break;
  138.         n += strlen(p+n);
  139.     }
  140.     return realloc(p, n+1);
  141. }
  142.  
  143.  
  144. /* By initializing this function pointer, systems embedding Python can
  145.    override the readline function. */
  146.  
  147. char *(*PyOS_ReadlineFunctionPointer) Py_PROTO((char *));
  148.  
  149.  
  150. /* Interface used by tokenizer.c and bltinmodule.c */
  151.  
  152. char *
  153. PyOS_Readline(prompt)
  154.     char *prompt;
  155. {
  156.     if (PyOS_ReadlineFunctionPointer == NULL) {
  157.             PyOS_ReadlineFunctionPointer = PyOS_StdioReadline;
  158.     }
  159.     return (*PyOS_ReadlineFunctionPointer)(prompt);
  160. }
  161.